package engine.display.Story.ActionControl 
{
	import engine.display.Story.StoryMoveEvent.StoryEvent;
	
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	/**
	 * ...
	 * @author WangHaiBo  www.woniu.com
	 */
	public class TimerTextField extends TextField 
	{
		private var showStr:String	=	"";
		private var timer:Timer;
		private var strIndex:int	=	0;
		private var _timerlenght:int	=	30
		public function TimerTextField() 
		{
			timer	=	new Timer(timerlenght);
			timer.addEventListener(TimerEvent.TIMER, timerHandle)
		}
		
		private function timerHandle(e:TimerEvent):void 
		{
			if (showStr	!= "") 
			{
				if (strIndex < showStr.length) 
				{
					super.text	=	showStr.slice(0, strIndex);
					strIndex++;
				}
				else
				{
					super.text	=	showStr;
					dispatchEvent(new StoryEvent(StoryEvent.TEXT_OVER));
					timer.stop();
				}

			}
			else
			{
				dispatchEvent(new StoryEvent(StoryEvent.TEXT_OVER));
				timer.stop();
			}
		}
		override public function get text():String 
		{
			return super.text;
		}
		
		override public function set text(value:String):void 
		{
			super.text	=	""
			strIndex	=	0;
			showStr		=	value;
			timer.start();
		}
		
		public function get timerlenght():int 
		{
			return _timerlenght;
		}
		
		public function set timerlenght(value:int):void 
		{
			_timerlenght = value;
			timer.delay	=	_timerlenght;
		}
		
		public function showAll():void
		{
			timer.stop();
			super.text	=	showStr;
			setTimeout(dispatch,500);
			
		}
		private function dispatch():void
		{
			dispatchEvent(new StoryEvent(StoryEvent.TEXT_OVER));
		}
		
	}

}